<?php
/**
 * Created by PhpStorm.
 * User: jungshen
 * Date: 2018/10/15
 * Time: 14:35
 */

namespace app\api\controller\user;


use app\api\controller\BasicUserApi;
use think\Db;
use think\Exception;

class Cases extends BasicUserApi
{
    /**
     * 获取用户案例列表
     * @return \think\response\Json
     */
    public function index(){
        try{
            $time=intval(input('time',0));
            $db=Db::name('member_case')->where('mid',UID);
            if($time==1){
                //一周内
                $db->where('addtime','>',strtotime("-7 day"));
            }elseif($time==2){
                //半年内
                $db->where('addtime','>',strtotime("-3 month"));
            }
            $list=$db->select();
            foreach ($list as $k=>&$v){
                $v['images']=explode('|',$v['images']);
                $v['addtime']=date('Y-m-d',$v['addtime']);
            }
            return json(['msg'=>'success','data'=>$list]);
        }catch (Exception $e){
            return json(['msg'=>$e->getMessage()],500);
        }
    }

    /**
     * 添加用户案例
     * @return \think\response\Json
     */
    public function add(){
        try{
            $post=$this->request->only(['title','images','desc','province'],'post');
            $validate=new \app\api\validate\Cases();
            if(!$validate->check($post)){
                return json(['msg'=>$validate->getError()],500);
            }
            $post['status']=1;
            $post['addtime']=$post['updatetime']=time();
            $post['mid']=UID;
            Db::name('member_case')->insert($post);
            return json(['msg'=>'添加成功']);
        }catch (Exception $e){
            return json(['msg'=>$e->getMessage()],500);
        }
    }

    /**
     * 编辑用户案例
     * @return \think\response\Json
     */
    public function edit(){
        try{
            $post=$this->request->only(['title','images','desc','id'],'post');
            $validate=new \app\api\validate\Cases();
            if(!$validate->check($post)){
                return json(['msg'=>$validate->getError()],500);
            }
            $post['status']=1;
            $post['updatetime']=time();
            Db::name('member_case')->update($post);
            return json(['msg'=>'修改成功']);
        }catch (Exception $e){
            return json(['msg'=>$e->getMessage()],500);
        }
    }

    /**
     * 删除用户案例
     * @return \think\response\Json
     */
    public function del()
    {
        try{
            $id=input('id');
            Db::name('member_case')->where('mid',UID)->where('id',$id)->delete();
            return json(['msg'=>'删除成功']);
        }catch(\Exception $e){
            return json(['msg'=>$e->getMessage()],500);
        }
    }
    /**
     * 获取案例详细信息
     * @return \think\response\Json
     */
    public function info()
    {
        try{
            $id=input('id');
            $case=Db::name('member_case')->where('mid',UID)->where('id',$id)->hidden('mid,status')->find();
            $case['images']=explode('|',$case['images']);
            return json(['msg'=>'success','data'=>$case]);
        }catch (Exception $e){
            return json(['msg'=>$e->getMessage()],500);
        }
    }
}